// pages/projectRecord/projectRecord.js
const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: "one",
    haveData1: false,
    haveData2: false,
    haveData3: false,
    tmp: {},
    firstTagData: [],
    secondTagData: [],
    thirdTagData: [],
  },

  //递归查询项目信息,再做处理
  searchProjectDetail(arr, i) {
    let that = this;
    db.collection('projects').where({
      _id: arr[i].project_id
    }).get({
      success: res => {
        if (res.data.length != 0) {
          //同名属性，后面覆盖前面
          Object.assign(arr[i], res.data[0]);
        }
        if (arr.length != i + 1) {
          return that.searchProjectDetail(arr, i + 1);
        } else { //递归出口

          let firstTagData = [];
          let secondTagData = [];
          let thirdTagData = [];

          for (let i = 0; i < arr.length; i++) {
            if (arr[i].status == "待处理") {
              firstTagData.push(arr[i]);
            } else if (arr[i].status == "已拒绝") {
              secondTagData.push(result[i]);
            } else if (arr[i].status == "已同意") {
              thirdTagData.push(arr[i]);
            }
          }

          if (firstTagData.length != 0) {
            that.setData({
              haveData1: true
            });
          }
          if (secondTagData.length != 0) {
            that.setData({
              haveData2: true
            });
          }
          if (thirdTagData.length != 0) {
            that.setData({
              haveData3: true
            });
          }

          that.setData({
            firstTagData,
            secondTagData,
            thirdTagData
          });

          // console.log(that.data);
        }
      },
      fail: res => {
        console.log(res);
      }
    })

  },

  navToProjectDetail(e) {
    wx.showLoading({
      title: '请求中',
    })
    console.log(e.currentTarget.dataset)
    let id = e.currentTarget.dataset.project_id;
    console.log(id);
    db.collection('projects').doc(id).get({
      success: res => {
        app.navTmpInfo = res.data;
        console.log(res.data);
        wx.navigateTo({
          url: '/pages/project_detail/project_detail',
        })
      },
      fail: res => {
        console.log(res);
      },
      complete: function () {
        wx.hideLoading();
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    // 查询用户申请参与的项目
    db.collection('wantJoinFile').where({
      _openid: app.personInfo._openid
    }).get({
      success: res => {
        if (res.data.length != 0) {
          console.log(res.data);
          //查询、合并、刷新数据
          that.searchProjectDetail(res.data, 0);
        }

      },
      fail: res => {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})